package com.fairytask.ui.mainUnit
{
	import com.fairytask.data.Modulator;
	import com.fairytask.data.RecordObject;
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.engine.define.CoolDefine;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.SystemTime;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.item.TimeShow;
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;

	/**
	 * 强化 升星 
	 * @author zhut
	 * 
	 */	
	public class UniverseUI extends UnitUI
	{
		private static var _self:UniverseUI;
		private var _model     :Modulator = Modulator.instance;
		public var imgBG		:MovieClip;   //物品图标背景
		public var img			:Clip;        //物品图标
		public var equipName	:Label;       //物品名称
		public var equipInfoBG	:MovieClip;   //物品信息背景
		public var upStarBtn	:Button;      //升星按钮
		public var upFullBtn	:Button;      //一键升满按钮
		public var successOdds	:Label;       //成功率     标签      
		public var	oddsPlus	:Label;       //成功率加成 标签
		public var info1		:Label;       //还少       标签
		public var info2		:Label;       //升级       标签
		public var info3		:Label;       //升星消耗   标签
		public var successData	:Label;       //成功率数值标签
		public var oddsData	:Label;       //成功率加成数值标签
		public var needStar	:Label;       //需要星星数标签
		public var upQuality	:Label;       //升级品质标签
		public var needSilver	:Label;       //需要银两值标签
		public var silverLab	:Label;       //银两        标签
		
		private var starBG    :Clip;        //星星显示背景
		private var maxStar   :int;         //最大星星数
		private var currStar  :int;         //当前星星数
		private var currColor :int          //当前品质
		private var silver:int;
		private var id:int;
		private var configId:int;
		private var starMode:Clip;
		private var currEquip:ViewObject;
		private var _coolTime:uint;
		private var _startTime:uint;
		private var upColorEqId:int;
		
		public var upQualityBtn:Button;
		public var upQualityAdds:Label;
		public var upQualityNum:Label;
		public var upQualityInfo:Label;
		public var consumeQuality:Label;
		public var showTime:TimeShow;      //冷却时间倒计时
		public var resetBtn:SimpleButton;  //刷新冷却时间按钮
		
		private var _coolArr:Array=[];
		private var _startArr:Array=[];
		
		private var consumeBG:Clip=new Clip();
		private  var item:Window;
		
		private var bindArr                 :Array = [];   //绑定属性
		private var _name                    :String;       //存储装备的名称
		private var fTime                    :Number;       //冷却开始时间
		private var cTime                    :Number;       //冷却持续时间
		private var propSilver               :int;          //玩家拥有的银两 
		private var data                     :ViewObject;
		private var isSuccess                :Boolean;     //升星是否成功
		
		public function UniverseUI()
		{
			init();
			initEvent();
			UIShow();
		}
		public static function getInstance():UniverseUI
		{
			if(_self==null)
			{
				_self=new UniverseUI();
			}
			return _self;
		}
		/**
		 * 强化界面显示
		 * @param isShow  是否选中物品。选中物品，显示物品属性，反之不显示
		 * @param isStar  是否升星。true：显示升星界面   false：显示升品界面
		 * 
		 */
		public function UIShow(isShow:Boolean=false,isStar:Boolean=false):void
		{
			img.visible       = isShow;
			equipName.visible = isShow;
			if(null != starBG)
			{
				starBG.visible= isShow;
			}
			StarUI(isShow && isStar);
			QualityUI(isShow &&(!isStar));
			consumeBG.visible = (isShow && (!isStar));
			if(!isShow)
				unBind();
		}
		/**
		 *初始化 
		 * 
		 */
		private function  init():void
		{
			consumeBG.height=50;
			consumeBG.width=200;
			consumeBG.x=35;
			consumeBG.y=300;
			windowBG.addChild(consumeBG);
			
			showTime = new TimeShow(0,"",null,1);
			showTime.x = 40;
			showTime.y = 355;
			windowBG.addChild(showTime);
		}
		
		/**
		 *按钮事件初始化 
		 * 
		 */
		private function initEvent():void
		{
			upStarBtn.addEventListener(MouseEvent.CLICK,onUpStar);
			upFullBtn.addEventListener(MouseEvent.CLICK,onUpFull);
			upQualityBtn.addEventListener(MouseEvent.CLICK,onUpColor);
			resetBtn.addEventListener(MouseEvent.CLICK,onReset);
			//_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_ADD_STAR_LOSE,upStarResult);
		}
		/**
		 *绑定属性 
		 * @param data
		 * @param viewID
		 * 
		 */
		public function initProp(data:ViewObject,viewID:int):void
		{
//			if(point ==data.objId)
//			{
//				return;
//			}
//			point = data.objId;
			if(data==null)
			{
				return;
			}
			unBind();
			this.data = data;
			//给服务端发送消息
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUTORMMSG_SELECT_EQUIP,data.objId);
			//绑定属性
			//data,bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Silver",getCapital);
			bindArr.push([data,"UpID",     bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"UpID",onAdd,onRemove)]);
			bindArr.push([data,"UpProb",   bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"UpProb",onAdd,onRemove)]);
			bindArr.push([data,"UpSilver", bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"UpSilver",onAdd,onRemove)]);
			bindArr.push([data,"Color",    bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"Color",onAdd,onRemove)]);
			bindArr.push([data,"Star",     bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"Star",onAdd,onRemove)]);
			bindArr.push([data,"MaxStar", bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"MaxStar",onAdd,onRemove)]);
			bindArr.push([data,"ConfigID",bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"ConfigID",onAdd,onRemove)]);
			bindArr.push([data,"UpItem",  bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"UpItem",onAdd,onRemove)]);
			bindArr.push([data,"Level",   bindManager.BindPropInViewObj(model.scene.GetView(viewID),data.ObjKey,"Level",onAdd,onRemove)]);
			
			//绑定冷却表
			bindManager.BindRecord(model.scene,RecordDefine.COOLREC,addCooltime,null);
			currEquip=data;
		}
		/**
		 *解除绑定 
		 * 
		 */
		private function unBind():void
		{
			for each(var bind:Array in bindArr)
			{
				bindManager.UnbindProp(bind.shift(),bind.shift(),bind.shift());
			}
		}
		/**
		 *获取玩家拥有的银两 
		 * @param key
		 * @param val
		 * 
		 */
		private function getCapital(key:String,val:int):void
		{
			propSilver = val;
			setMoneyColor();
		}
		/**
		 *绑定冷却时间 
		 * @param key
		 * @param val
		 * 
		 */
		private function addCooltime(key:String,val:RecordObject):void
		{
			if(val.GetProperties("0")==CoolDefine.COOL_DOWN_EQUIP)
			{
				bindManager.BindObjProp(val,"1",addCool);
				bindManager.BindObjProp(val,"2",addCool);
				bindManager.BindObjProp(val,"3",addCool);
			}
		}
		/**
		 *获取冷却时间 
		 * @param key
		 * @param val
		 * 
		 */
		private function addCool(key:String,val:int):void
		{
			switch(key)
			{
				case "1"://冷却时间
				{
					cTime = val;
					break;
				}
				case "2"://冷却开始时间
				{
					fTime = val;
					break;
				}
				case "3"://冷却状态  1：正常   2：强制
				{
					showTime.styleColor(val==2?"ff0000":"00ff2d");
				}
			}
			if(cTime>-1 && fTime>-1)
			{
				showTime.time = SystemTime.getLeftTime(fTime*1000,cTime*1000)/1000-1;
				cTime = fTime = -1;
			}
		}
		/**
		 *添加星形图标 
		 * @param num 当前星星数量
		 * 
		 */
		private function  initInfo(num:int):void
		{
			if(starBG!=null)
			{
				windowBG.removeChild(starBG);
			}
			starBG=new Clip();
			starBG.x=30;
			starBG.y=100;
			starBG.height=80;
			starBG.width=200;
			windowBG.addChild(starBG);
			
			for(var i:int=1;i<=num;i++)
			{
				createStarMode(i);
			}
		}
		/**
		 *显示出星星 
		 * @param index 星星索引值
		 * 
		 */
		private function createStarMode(index:int):void
		{
			var row:int;
			var col:int;
			if(index%10!=0)
			{
				row=int(index/10)+1;
			}
			else
			{
				row=int(index/10);
			}
			if(index%10!=0)
			{
				col=int(index%10);
			}
			else
			{
				col=10;
			}
			starMode=new Clip();
			starMode.source=ResConfig.pngResPath+"tips_star2.png";
			starMode.width = 23;
			starMode.height = 23;
			starMode.x=(col-1)*25;
			starMode.y=(row-1)*25;
			starBG.addChild(starMode);
		}
		
		/**
		 *判断升星银两是否够以显示不同的颜色 
		 * 
		 */
		private function setMoneyColor():void
		{
			needSilver.htmlStrByBorder=StaticIni.getSwfLangStrVar(propSilver<silver?"1000000037":"1000000034",[silver]);
		}
		/**
		 *绑定属性 
		 * @param key
		 * @param val
		 * 
		 */
		private function onAdd(key:String,val:int):void
		{
			switch(key)
			{
				case "Color":
					currColor=val;
					//upQuality.htmlStrByBorder=TipsTools.link_color(StaticIni.getSwfLangStr(1000000188+currColor+""),currColor+1);
					break;	
				case "UpID":
					id=val;
					break;
				case "UpProb":
					successData.htmlStrByBorder=int(val/100)+"%"
					oddsData.htmlStrByBorder="+0%";
					break;
				case "UpSilver":
					silver=val;
					setMoneyColor();
					break;
				case "Star":
					currStar=val;
					initInfo(currStar);
					break;
				case "MaxStar":
					maxStar=val;
					//needStar.htmlStrByBorder=TipsTools.link_color(StaticIni.getSwfLangStr((maxStar-currStar)*2+1000000146+""),currColor+1);
					break;
				case "ConfigID":
					this.configId = val;
					_name = StaticIni.getIniVar(val,"Name");
					var iconId:int = int(StaticIni.getIniVar(val,"Icon"));
					img.source=ResConfig.iconPath+(iconId)+ResConfig.iconImg;
					img.addEventListener(MouseEvent.ROLL_OVER,function(mevt:MouseEvent):void
					{
						TipsManage.ShowItemMaxTips(iconId,data);
					});
					img.addEventListener(MouseEvent.ROLL_OUT,function(mevt:MouseEvent):void
					{
						ToolTips.hide();
					});
					break;
				case "UpItem":
					if(currStar==maxStar)
					{
						upColorEqId=val;
					}
					break;
				case "Level":
					//_name	=	TipsTools.getRefineName(val) + _name;
					//equipName.htmlStrByBorder = TipsTools.onlyColor(_name,currColor);
					break;
			}
		}
		/**
		 *升品UI显示隐藏 
		 * @param isShow
		 * 
		 */
		public function QualityUI(isShow:Boolean=false):void
		{
			upQualityBtn.visible=upQualityAdds.visible=upQualityNum.visible=upQualityInfo.visible=consumeQuality.visible=isShow;
		}
		/**
		 *升星UI显示隐藏 
		 * @param isShow
		 * 
		 */
		public  function StarUI(isShow:Boolean=false):void
		{
			upStarBtn.visible=upFullBtn.visible=isShow;
			successOdds.visible=oddsPlus.visible=isShow;
			info3.visible=isShow;
			successData.visible=oddsData.visible=isShow;
			info1.visible=info2.visible=isShow && (currColor != 6);
			needStar.visible=upQuality.visible=isShow && (currColor != 6);
			showTime.visible=resetBtn.visible=isShow;
			silverLab.visible=needSilver.visible=isShow;
		}
		private  function onRemove(key:String,val:String):void
		{
		}
		
		private function onUpStar(e:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDSTAR,1,Player.getInstance().id,currEquip.objId);
		}
		private function onUpFull(e:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDSTAR,2,Player.getInstance().id,currEquip.objId);
			
		}
		private function onUpColor(e:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDQUALITY,currEquip.objId,Player.getInstance().id);
		}
		private function onReset(e:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_COOLDOWN,CoolDefine.COOL_DOWN_EQUIP);
		}
		/**
		 *获取升品道具 
		 * @param e
		 */
		/*public function getEquip(e:CustomToClientEvent):void
		{
			UIShow(true,e.pList.shift() == 1);
			while(consumeBG.numChildren>0)
			{
				consumeBG.removeChildAt(0);
			}
			var length:int = e.pList.length;
			for(var i:int=0;i<length;i+=3)
			{ 
				showEquip(int(i/3),e.pList.shift(),e.pList.shift(),e.pList.shift());
			}
		}*/
		/**
		 *显示升级道具 
		 * @param index   索引值 第index个道具
		 * @param Id      道具id
		 * @param needNum 升级需要数量
		 * @param allNum  玩家拥有数量
		 * 
		 */
		private function showEquip(index:int,Id:int,needNum:int,allNum:int):void
		{
			//创建图片
			var icon:Clip = new Clip();
			icon.source = ResConfig.iconPath + (StaticIni.getIniVar(Id,"Icon")) + ResConfig.iconImg;
			icon.width = icon.height = 45;
			icon.x = index * 48;
			icon.y = 2;
			icon.addEventListener(MouseEvent.CLICK,function(mevt:MouseEvent):void
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_BUY_ITEM,configId);
			});
			icon.addEventListener(MouseEvent.ROLL_OVER,function(mevt:MouseEvent):void
			{
				TipsManage.ShowItemTips(Id);
			});
			icon.addEventListener(MouseEvent.ROLL_OUT,function(mevt:MouseEvent):void
			{
				ToolTips.hide();
			});
			//创建数量标签
			var numLab:Label = new Label();
			numLab.htmlStrByBorder = "<font color='"+(allNum<needNum?"#ff0000":"#00ff2d")+"'>"
				                     +allNum+"</font>" + "<font color='#ffffff'>/" + needNum+"</font>";
			numLab.x = 20 + icon.x;
			numLab.y = 27;
			consumeBG.addChild(icon);
			consumeBG.addChild(numLab);
		}
		
		/*****以下内容为升星成功或失败播放动画*****/
		
		/**
		 *获取播放图片 
		 * @param val
		 * 
		 */
		private function playIsSuccess(val:Boolean):void
		{
			var res:String = ResConfig.pngResPath + (val?"victory.png":"lose.png");
			//QueueLoader.StartLoad(res,onLoad);
		}
		/**
		 *加载图片 
		 * @param val
		 * 
		 */
		private function onLoad(val:Bitmap):void
		{
			var mc:Bitmap = val;
			mc.smoothing = true;
			mc.x=80;
			mc.y=300;
			addChild(mc);
			playAnime(mc);
		}
		/**
		 *开始播放 
		 * @param mc
		 * 
		 */
		private function playAnime(mc:Bitmap):void
		{
			TweenLite.to(mc, 2.4, {x:80, y:120, ease:Cubic.easeOut,onComplete:onEndPoint,onCompleteParams:[mc]});
		}
		/**
		 *播放完成后消失 
		 * @param mc
		 * 
		 */
		private function onEndPoint(mc:Bitmap):void
		{
			TweenLite.to(mc, 0.2, {onComplete:playImg,onCompleteParams:[mc]});
		}
		
		/**
		 *消失后装备图标闪光 
		 * @param mc
		 * 
		 */
		private function playImg(mc:Bitmap):void
		{
			if(null != mc.parent)
			{
				mc.parent.removeChild(mc);
			}
			if(isSuccess)
			{
				var bgTimeLine:TimelineLite = new TimelineLite({repeat:3});
				bgTimeLine.append(TweenMax.to(img, 0.5, {glowFilter:{color:getColor(currColor), alpha:0.7, blurX:20, blurY:20, strength:2, quality:1},repeat:3,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}}}));
				bgTimeLine.pause();
				bgTimeLine.play();
			}
		}
		/**
		 *获取升星结果 
		 * @param evt
		 * 
		 */
		/*private function upStarResult(evt:CustomToClientEvent):void
		{
			isSuccess = evt.pList.shift() == 1;
			playIsSuccess(isSuccess);
		}*/
		/**
		 *根据装备品质获取图标闪光颜色 
		 * @param val
		 * @return
		 * 
		 */
		private function getColor(val:int):int
		{
			switch(val)
			{
				case 1:
					return 0xffffff;
				case 2:
					return 0x00ff2d;
				case 3:
					return 0x00e4ff;
				case 4:
					return 0xa518ff;
				case 5:
					return 0xff6600;
				case 6:
					return 0xffe400;
			}
			return 1;
		}
	}
}